package student.test.DAO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import student.test.entity.Student;

import java.util.List;

@Repository
public class StudentDAO {
    @Autowired
    JdbcTemplate jdbcTemplate;
    public int addStudent(Student student){
        return jdbcTemplate.update("INSERT INTO student(name,sex,age,point) VALUES (?,?,?,?)", student.getName(),student.getSex(),student.getAge(),student.getPoint());
    }
    public int updateStudent(Student student){
        return jdbcTemplate.update("UPDATE student SET name=?,sex=?,age=?,point=? WHERE id=?", student.getName(),student.getSex(),student.getAge(),student.getPoint(),student.getId());
    }
    public int deleteStudentById(Integer id){
        return jdbcTemplate.update("DELETE FROM student WHERE id=? ",id);
    }
    public Student getStudentById(Integer id){
        return jdbcTemplate.queryForObject("select * from student where id = ?", new BeanPropertyRowMapper<>(Student.class),id);
    }
    public List<Student> getAllStudents(){
        return jdbcTemplate.query("select * from student",new BeanPropertyRowMapper<>(Student.class));
    }
}
